# -*- coding: utf-8 -*-
# coding=utf-8
# coding: utf-8
from langchain_openai import ChatOpenAI

from agent.AutoGPTAgent import AutoGPTAgent
from tools import document_qa_tool, document_generation_tool, email_tool, excel_inspection_tool, \
    directory_inspection_tool, finish_placeholder
from tools.PythonTool import ExcelAnalyser
from langchain_community.chat_message_histories.in_memory import ChatMessageHistory
# 加载 .env 到环境变量
from dotenv import load_dotenv, find_dotenv

_ = load_dotenv(find_dotenv())


def launch_agent(agent):
    human_icon = "\U0001F468"
    ai_icon = "\U0001F916"
    chat_history = ChatMessageHistory()
    while True:
        task = input(f"{ai_icon}：有什么可以帮您？\n{human_icon}：")
        if task.strip().lower() == "quit":
            break
        reply = agent.run(task, chat_history, verbose=True)
        print(f"{ai_icon}：{reply}\n")


def main():
    # 语言模型
    llm = ChatOpenAI(
        model="gpt-3.5-turbo",
        temperature=0,
        model_kwargs={
            "seed": 42
        },
    )
    # 自定义工具集
    tools = [
        document_qa_tool,
        document_generation_tool,
        email_tool,
        excel_inspection_tool,
        directory_inspection_tool,
        finish_placeholder,
        ExcelAnalyser(
            prompt_file="./prompts/tools/excel_analyser.txt",
            verbose=True
        ).as_tool()
    ]

    # 定义智能体
    my_agent = AutoGPTAgent(
        llm=llm,
        tools=tools,
        work_dir="./data",
        main_prompt_file="./prompts/main/main.txt",
        max_thought_steps=20,
    )


    # 运行智能体
    launch_agent(my_agent)


if __name__ == "__main__":
    main()
